package xyz.spoplar.aidlclient;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xunce.aidl.IMyAidlInterface;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText arg1;

    private  EditText arg2;

    private Button button;

    private Button jump;

    private Button lklbtn;

    private TextView text;

    Intent intent;

    IMyAidlInterface iMyAidl;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("TAG","connected");
            iMyAidl = IMyAidlInterface.Stub.asInterface(service);
            Log.d("TAG",iMyAidl.toString());

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("TAG","disconnected");
            iMyAidl = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        arg1 = (EditText) findViewById(R.id.arg1);
        arg2 = (EditText) findViewById(R.id.arg2);
        button = (Button) findViewById(R.id.button);
        text = (TextView) findViewById(R.id.textView3);

        jump = (Button) findViewById(R.id.jump);

        button.setOnClickListener(this);
        jump.setOnClickListener(this);

        lklbtn = (Button) findViewById(R.id.lklbtn);

        lklbtn.setOnClickListener(this);

        Log.i("TAG","created");
        bindService();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //获取到服务器
                int num=0;
                try {
                     num = iMyAidl.add(Integer.parseInt(arg1.getText().toString()),Integer.parseInt(arg2.getText().toString()));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                text.setText(num+"");

                break;
            case R.id.jump:
                Log.d("TAG","jump");
                Toast.makeText(MainActivity.this, "jump", Toast.LENGTH_SHORT).show();
                //新建一个显式意图，第一个参数为当前Activity类对象，第二个参数为你要打开的Activity类
                Intent intent2 = new Intent(MainActivity.this,SecondActivity.class);
                //用Bundle携带数据
                Bundle bundle = new Bundle();
                bundle.putString("name","tinyphp");
                intent2.putExtras(bundle);
                //startActivity(intent2);
                startActivityForResult(intent2,0);
                break;
            case R.id.lklbtn:
                Intent intent3 = new Intent();
                intent3.setComponent(new ComponentName("com.lkl.cloudpos.payment","com.lkl.cloudpos.payment.activity.MainMenuActivity"));
                Bundle bundle1 = new Bundle();
                bundle1.putString("msg_tp","0200"); //0200消费，0300查询
                bundle1.putString("pay_tp","0"); //0卡，1扫码（微信，支付宝，银联钱包）

                bundle1.putString("proc_tp","00");
                bundle1.putString("proc_cd","000000"); //000000消费，200000消费撤销 6800000扫码撤销
                bundle1.putString("amt","100.01");
                bundle1.putString("order_no","123456");
                bundle1.putString("appid","xyz.spoplar.aidlclient");
                bundle1.putString("time_stamp", Long.toString(Calendar.getInstance().getTimeInMillis()));
                bundle1.putString("order_info","测试订单");
                bundle1.putString("print_info","hello");
                bundle1.putString("return_type","1");
                intent3.putExtras(bundle1);
                startActivityForResult(intent3,1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK && requestCode ==0){
            Bundle bundle = data.getExtras();
            String name = bundle.getString("send");
            Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
        }else if(resultCode==Activity.RESULT_OK && requestCode==1){

            Bundle bundle1 = data.getExtras();
            String msg_tp = bundle1.getString("msg_tp");
            // 1-微信
//                2-支付宝
//                3-银联钱包
//                4-百度钱包
//                5-京东钱包
//                6-拉卡拉钱包
            String pay_tp = bundle1.getString("pay_tp");
            String refernumber = bundle1.getString("refernumber");
            String order_no = bundle1.getString("order_no");
            String time_stamp = bundle1.getString("time_stamp");
            String reason = bundle1.getString("reason");


        }else if(resultCode==Activity.RESULT_CANCELED && requestCode == 1){

        }

    }

    private void bindService() {

        intent= new Intent();
        intent.setComponent(new ComponentName("com.xunce.lkltest","com.xunce.lkltest.IMyAidlService"));

        bindService(intent,conn, Context.BIND_AUTO_CREATE);
        Log.i("TAG","bindService");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

//    private void bindService1() {
//        intent= new Intent(
//        intent.setComponent(new ComponentName("com.lkl.cloudpos.payment","com.lkl.cloudpos.payment.activity.MainMenuActivity"));
//
//        startActivityForResult(intent,);
//        Log.i("TAG","bindService");
//    }


}
